package core.resources
{
import core.Configuration;

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;

import framework.core.UIWidget;

import utils.URLUtil;

/**
 * SkinResourceManager
 * @author jliang
 * 
 */
public class SkinResourceTask extends LoadTaskBase
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function SkinResourceTask()
	{
		super();
	}
	
	override public function start():void
	{
		var url:String = Configuration.skinPath + "main_ui_skin.swf";
		
		var loader:Loader = new Loader();
		loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadedHandler);
		loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
		loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loadErrorHandler);
		var context:LoaderContext = new LoaderContext(false, new ApplicationDomain(), null);
		loader.load(URLUtil.getRequest(url), context);
	}
	
	private function loadedHandler(event:Event):void
	{
		var info:LoaderInfo = LoaderInfo(event.target);
		info.removeEventListener(Event.COMPLETE, loadedHandler);
		info.removeEventListener(IOErrorEvent.IO_ERROR, loadErrorHandler);
		info.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loadErrorHandler);
		
		UIWidget.registerAppDomain(info.applicationDomain);
		
		this.complete();
	}
	
	private function loadErrorHandler(event:Event):void
	{
		trace(event.toString());
	}
}
}










